package com.blog.check.check;

import com.blog.check.resultenum.ProfileChkRsEnum;
import com.blog.check.resultenum.UserChkRsEnum;
import com.blog.dao.domain.UserDO;
import com.blog.dao.query.UserQuery;
import com.blog.form.ProfileForm;
import com.blog.form.UserForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 用户检查
 *
 * @author 泡面ing
 * @date 2021/11/17
 */
@Service
public class UserCheck extends BaseCheck{
//    public static final String PW_PATTERN = "^(?=.*[0-9].*).{4,}$";

    /**
     * check for list
     *
     */
    String checkNumber = "1";
    public CheckResult checkList(UserQuery userQuery){
        // logical check for list
        return checkListLogic(userQuery);
    }

    private CheckResult checkListLogic(UserQuery userQuery) {

        List<UserDO> userDOList = userDAO.list(userQuery);
        UserDO userDO = userDAO.get(getUsername());
        if (getUsername() == null){
            return CheckResult.failure(UserChkRsEnum.USER_IS_NOT_LOGIN);
        }
        if (!userDO.getJurId().equals(checkNumber)){
            return CheckResult.failure(UserChkRsEnum.INSUFFICIENT_USER_PERMISSIONS);
        }
        if (ObjectUtils.isEmpty(userDOList)){
            return CheckResult.failure(UserChkRsEnum.ILLEGAL_USER_NAME_NOT_EXIST,userQuery.getUserName());
        }
        return CheckResult.success();
    }
    /**
     * check for save
     *
     */
    public CheckResult checkSave(UserForm userForm){
        // Non-logical check for save
        CheckResult checkResult = checkSaveNonLogic(userForm);
        if (checkResult.getCode() != CheckResult.success().getCode()){
            return checkResult;
        }
        // logical check for save
        checkResult = checkSaveLogic(userForm);
        return  checkResult;
    }

    private CheckResult checkSaveLogic(UserForm userForm) {

        if (StringUtils.isBlank(userForm.getUserName())){
            return CheckResult.failure(UserChkRsEnum.USER_NAME_NOT_CAN_BE_NULL);
        }
        if (StringUtils.isBlank(userForm.getPassword())){
            return CheckResult.failure(UserChkRsEnum.PASSWORD_NOT_CAN_BE_NULL);
        }
        ProfileForm profileForm = userForm.getProfile();
        if (StringUtils.isBlank(profileForm.getNickName())){
            return CheckResult.failure(ProfileChkRsEnum.ADD_ILLEGAL_NICKNAME_MUST_NOT_BE_NULL);
        }
        return CheckResult.success();
    }

    private CheckResult checkSaveNonLogic(UserForm userForm) {

        if (!isValidMobileNo(userForm.getPhoneNumber())&& StringUtils.isNotBlank(userForm.getPhoneNumber())){
            return CheckResult.failure(UserChkRsEnum.ISUNIT_PHONE_NUMBER,userForm.getPhoneNumber());
        }
        if (!isVaildEmail(userForm.getEmail())&& StringUtils.isNotBlank(userForm.getEmail())){
            return CheckResult.failure(UserChkRsEnum.ISUNIT_EMAIL_NUMBER,userForm.getEmail());
        }

        UserDO userDO = userDAO.get(userForm.getUserName());
        if (!ObjectUtils.isEmpty(userDO)){
            return CheckResult.failure(UserChkRsEnum.USER_NAME_ALREADY_EXISTS,userForm.getUserName());
        }

        return CheckResult.success();
    }

    public CheckResult checkUpdate(UserForm userForm){
        return checkUpdateNonLogic(userForm);
    }

    private CheckResult checkUpdateNonLogic(UserForm userForm) {
        if (!isValidMobileNo(userForm.getPhoneNumber())){
            return CheckResult.failure(UserChkRsEnum.ISUNIT_PHONE_NUMBER,userForm.getPhoneNumber());
        }
        if (!isVaildEmail(userForm.getEmail())){
            return CheckResult.failure(UserChkRsEnum.ISUNIT_EMAIL_NUMBER,userForm.getEmail());
        }
        return CheckResult.success();
    }

    public CheckResult checkDisable(String username) {
        UserDO userDO = userDAO.get(getUsername());
        if (!userDO.getJurId().equals(checkNumber)){
            return CheckResult.failure(UserChkRsEnum.INSUFFICIENT_USER_PERMISSIONS);
        }
        userDO = userDAO.get(username);
        if (userDO == null) {
            return CheckResult.failure(UserChkRsEnum.ILLEGAL_USER_NAME_NOT_EXIST);
        }
        return CheckResult.success();
    }
}
